package com.gitee.ccr.face.impl;

import com.gitee.ccr.face.UserBiz;
import com.gitee.ccr.face.UserService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "childBizImpl")
    private UserBiz childBiz;
    @Resource(name = "youngBizImpl")
    private UserBiz youngBiz;
    @Resource(name = "olderBizImpl")
    private UserBiz olderBiz;

    private final Map<Integer, UserBiz> roleMap = Maps.newHashMap();

//    private ImmutableMap<Integer, UserBiz> immutableMap;

    @PostConstruct
    public void init() {
        roleMap.put(1, childBiz);
        roleMap.put(2, youngBiz);
        roleMap.put(3, olderBiz);
//        ImmutableMap<Integer, UserBiz> immutableMap =
//                ImmutableMap.<Integer, UserBiz>builder().put(1, childBiz).put(2, youngBiz).put(3, olderBiz).build();
    }

    @Override
    public void register() {
        // do something common
        int personType = 1;
        Optional.ofNullable(roleMap.get(personType))
                .orElseThrow(() -> new IllegalArgumentException("xx")) // 抛出一个运行时异常（可以自定义一个全局RuntimeException，这里仅作示例）
                .register();
    }

    @Override
    public void update() {

    }
}